MC-10 Operation and Language Reference Manual Chapter 14

14/STRING FUNCTIONS You've already been introduced to how Micro Color BASIC uses the dollar sign (remember string variables?), but the MC-10 also recognizes several statements which have the suffix $. The INKEY$ Function Sometimes, when you are running a program, you want the program to pause execution until you "manually" tell it to continue. INKEY$ lets you do this. At all times during program execution, the MC-10 keeps track of the last key pressed since you typed RUN. It stores this character in a place we'll call the "key latch". If you haven't pressed any keys, then the latch is empty.
INKEY$
The INKEY$ function returns this character as a string value and empties the latch. If a key has not been pressed, INKEY$ returns a "null" string. [BREAK] is the only key INKEY$ will not respond to. Use INKEY$ like this: 10 FOR X = 1 TO 100 20 PRINT X 30 A$ = INKEY$: IF A$ = "" THEN 30 40 NEXT X 50 END Important Note! Be sure there is not a space between the quotation marks in line 30. If a space is there, the string is not a "null" string since it contains a space ("" is right; " " is wrong). When you RUN the program, it will print the first number and wait for you to press any key except [BREAK] to go on to the next number. If you delete line 30, the numbers will be displayed so fast you won't be able to read them. INKEY$ slows this down so that you control when they're printed. Line 30 works like this: You've assigned the INKEY$ function to the string variable A$. The second part of the line says that if A$ is a null string (""), then stay at the current line. If a key has been pressed (that is, if A$ equals a character), then drop down to the next line. INKEY$ can also be used to test greater than or less than values. 10 A$ = INKEY$: IF A$ < > "" THEN 50 20 PRINT "YOU HAVEN'T PRESSED A KEY." 30 GOTO 10 50 PRINT "THE KEY YOU PRESSED IS ";A$ 60 END In this instance, INKEY$ tells Micro Color BASIC to look at the keyboard to see if you have pressed anything. If you haven't pressed a key, the Computer continues to tell you that nothing has been pressed. When you do press a key, it tells you which key you pressed and then stops execution. If you changed the <> signs to =, the program will immediately loop to line 50 because it is testing for null strings. There Has To Be An Easier Way... To enter INKEY$ without typing in the entire word, type: [CONTROL][P] The ASC and CHR$ Statements You've been told before that Computers are dumb machines. They're so dumb, in fact, that they don't even know what the letters A and B are. Well, if that's so, how does the Computer know that you want the letter A when you press the [A] key? Because the Computer reads everything that is entered from the keyboard in terms of code numbers. Every keyboard character has been assigned a number called the ASCII (pronounced ASK-ee) code. The letter A, for instance, is code 65. Lowercase A (a) is code 97. (That's 32 greater than the uppercase A. All lowercase letter codes are 32 greater than the uppercase equivalent.) How can you find out what these codes are? For starters, you can look in Appendix C of this book where you'll find a complete list of all ASCII codes the MC-10 recognizes. Another way to find out a character's ASCII code is to use the ASC function:
ASC (string) string is a string variable, constant, or expression. Null strings are not allowed.
ASC returns the ASCII code for the first character of the specified string. For instance, in the Immediate Mode type: PRINT ASC ("A") [ENTER] and the TV will display: 65 OK which is the ASCII code for A. Now type: PRINT ASC ("ALBATROSS") [ENTER] and the TV will again display: 65 OK since only the first letter of the string is returned. Here's one way ASC can be used in a program: 10 INPUT "TYPE IN A WORD";A$ 20 INPUT "TYPE IN A DIFFERENT WORD";B$ 30 IF A$ = B$ THEN 20 40 IF A$ < B$ THEN F$ = A$: S$ = B$: GOTO 60 50 F$ = B$: S$ = A$ 60 PRINT F$; " PRECEDES"; S$; " ALPHABETICALLY." 70 PRINT: GOTO 10 This program uses ASC to read the first letter of the two words you type in, then tells you which of the two has the greater ASC value. The one with the greater value comes later in the alphabet. The CHR$ Statement A function you can use in conjunction with ASC is CHR$. This statement is the inverse of ASC. When you enter an ASCII code, CHR$ returns the character the code represents.
CHR$ (code) code is a numeric expression representing an ASCII code between 0 and 255.
In the Immediate Mode, type this: PRINT CHR$(65) [ENTER] and the TV will display: A OK Now type: PRINT CHR$(155) [ENTER] and the TV will display: (GRAPHICS CHAR HERE) OK which is a Graphics Character. CHR$ will let you display characters from a program that aren't normally displayed on the TV. For instance, you can enter a Graphics character directly from the keyboard into a program. To generate a displayable Graphics character whose color is different from cursor color, use CHR$ and specify the character's code. Remember, [SHIFT][A] etc. will produce graphics character of same color as cursor's. 10 FOR X = 150 TO 160 20 PRINT CHR$(X); " "; 30 NEXT X 40 END This will produce the ten Graphics Characters from ASCII codes 150 to 160. When you have a printer connected to the MC-10, CHR$ can be used to perform certain operations such as backspacing, repeat printing, etc. Use LPRINT CHR$(code) if you are performing specific operations with the printer. See your printer owner's manual for details on codes the printer uses. The STR$ Statement The STR$ function converts a number to a string value.
STR$ (number) number is a numeric value.
Normally, Micro Color BASIC will return a Type Mismatch Error if you try to use a number as a string value. However, STR$ lets you get around this problem. 10 INPUT "TYPE A NUMBER";N 20 A$ = STR$(N) 30 PRINT A$; " IS NOW A STRING." 40 END The LEN Function The LEN (length) statement returns the number of characters (letters, numbers, and spaces) in a string.
LEN (string) string is a string variable, constant, or expression.
To see how LEN works, type in this short program: 10 INPUT "TYPE IN THREE SHORT WORDS";A$ 20 PRINT "WHAT YOU TYPED IN USES"; LEN(A$); " SPACES" 30 END Notice that the spaces in between the words are counted as well. A function like LEN can be very important with a limited memory Computer like the MC-10. You can use it to limit how many characters (hence, how much memory) are entered into a line: 10 INPUT "USE ONLY 3 LETTER ABBREVIATIONS FOR MONTH";M$ 20 IF LEN(M$) > 3 THEN 10 30 PRINT "THE MONTH IS ";M$ 40 END LEFT$, MID$, and RIGHT$ Micro Color BASIC has three functions that examine the parts of a string: LEFT$ (which gets the left portion of the string; MID$ (which gets the middle portion); and RIGHT$ (which gets the right portion).
LEFT$ (string, length) string is a string variable, constant, or expression length tells how many characters to look for in string.
RIGHT$ (string, length) string is a string variable, constant, or expression length tells how many characters to look for in string.
MID$ (string, position, length) string is a string variable, constant, or expression position tells where the mid-portion starts and can be any number from 1 (the first character in the string) up to the total length of the string. length tells how many characters to look for in string.
Use LEFT$ and RIGHT$ first. 10 INPUT "TYPE IN A WORD";W$ 20 PRINT "THE FIRST LETTER IS "; LEFT$(W$,1) 30 PRINT "THE LAST TWO LETTERS ARE "; RIGHT$(W$,2) For this example, type in WORD [ENTER] when prompted. The TV will then display: THE FIRST LETTER IS W THE LAST TWO LETTERS ARE RD MID$, on the other hand, checks the middle portion of a string after you tell it where to start counting. Add this line to the above program. 40 PRINT :THE MID PART OF YOUR WORD IS ";MID$(W$,2,2) Now when you RUN the program and type in WORD, the TV will display: THE FIRST LETTER IS W THE LAST TWO LETTERS ARE RD THE MID PART OF YOUR WORD IS OR Line 20 uses LEFT$ to specify the word (string) you want to examine and how many letters in that word you want to look at. Line 30 uses RIGHT$ for the same purpose. Line 40 uses MID$ to specify the word, which part of the word to start counting at, and how many letters to count. Here's a practical example of three of the four string functions at work: 10 A$ = "CHANGE A SENTENCE." 20 B$ = "IT'S EASY TO " 30 C$ = B$ + " " + A$ 40 PRINT C$ 50 Y = LEN ("A SENTENCE") 60 FOR X = 1 TO LEN(C$) 70 IF MID$(C$,X,Y) = "A SENTENCE" THEN 90 80 NEXT X 85 END 90 D$ = LEFT$(C$,X-1) 100 E$=D$ + "ANYTHING YOU WANT" 110 PRINT E$ The CLEAR Statement When using strings, DATA statements, or creating arrays with Micro Color BASIC programs, you'll sometimes need to reserve memory at the beginning of your program. CLEAR does this for you.
CLEAR amount amount is the amount of memory that you need to reserve for your program.
When you do not use the CLEAR statement, the MC-10 assumes you will need only enough string space to store 100 characters. Unless you change this with CLEAR, you'll get an Out of String space error when you try to store more than 100 characters. CLEAR will reserve 100 character spaces for your strings. CLEAR 500 will reserve 500 characters.

Comments should be directed to the following email address:
dsbrain@neosplice.com